#include "plat_types.h"

#if USE_LV_DM != 0

#include "dm_cmid.h"
#include "dm_register.h"

static int cell_ind_flag = 0;
static UINT32 cell_id = 0;

static void DM_LteCellInfoInd(app_adp_lte_cells_t *cell_list)
{
    cell_ind_flag = 1;
    cell_id = cell_list->scell.cellid;
    printf("[DM] %s: cell_id=%d\n", __func__, cell_id);
}

static void DM_GsmCellInfoInd(app_adp_gsm_cells_t *cell_list)
{
    cell_ind_flag = 1;
    cell_id = cell_list->scell.cell_id;
    printf("[DM] %s: cell_id=%d\n", __func__, cell_id);
}

int DM_IsSimExist(int nSimID)
{
    UINT8 sim_exist = 0;

    if(0 == nSimID) {
        sim_exist = app_adaptor_get_sim_status_req();
    }

    return sim_exist;
}

char* DM_NwGetImei (CMID_SIM_ID nSimID)
{
    static char imei[16];
    app_adp_get_imei_req(imei);
    return imei;
}

char* DM_NwGetMEID(VOID)
{
    return "";
}

char* DM_NwGetIccid(CMID_SIM_ID nSimID)
{
    static char iccid[ICCID_LEN + 1];
    char *p_iccid = NULL;

    p_iccid = app_adaptor_get_iccid_info_req();
    strncpy(iccid, p_iccid, ICCID_LEN + 1);
    UI_FREE(p_iccid);

    return iccid;
}

char* DM_NwGetImsi(CMID_SIM_ID nSimID)
{
    static char imsi[IMSI_LEN + 1];
    app_adp_get_imsi_req(imsi);
    return imsi;
}

UINT32 DM_NwGetCellid(CMID_SIM_ID nSimID)
{
    UINT32 cellid = 0;

    app_adaptor_lte_cell_info_ind_bind(DM_LteCellInfoInd);
    app_adaptor_gsm_cell_info_ind_bind(DM_GsmCellInfoInd);

    app_adaptor_get_cell_info_req(GET_CELL_INFO_ONCE_MODE);

    for(int i=0; i<100; i++) {
        uos_sleep(20);  // 100 ms

        if(1 == cell_ind_flag) {
            cellid = cell_id;
            cell_id = 0;
            cell_ind_flag = 0;
            break;
        }
    }

    return cellid;
}

UINT32 DM_GetDataSimCard(VOID)
{
    return 0;
}

BOOL DM_IsDataConnectOK(VOID)
{
    return TRUE;
}

#endif
